package com.only4play.productcenter.domain.goods;

import com.only4play.common.annotation.FieldDesc;
import com.only4play.common.exception.BusinessException;
import com.only4play.productcenter.infrastructre.constants.Constants;
import com.only4play.codegen.processor.api.GenCreateRequest;
import com.only4play.codegen.processor.api.GenFeign;
import com.only4play.codegen.processor.api.GenQueryRequest;
import com.only4play.codegen.processor.api.GenResponse;
import com.only4play.codegen.processor.api.GenUpdateRequest;
import com.only4play.codegen.processor.controller.GenController;
import com.only4play.codegen.processor.creator.GenCreator;
import com.only4play.codegen.processor.mapper.GenMapper;
import com.only4play.codegen.processor.query.GenQuery;
import com.only4play.codegen.processor.repository.GenRepository;
import com.only4play.codegen.processor.service.GenService;
import com.only4play.codegen.processor.creator.IgnoreCreator;
import com.only4play.codegen.processor.updater.IgnoreUpdater;
import com.only4play.codegen.processor.service.GenServiceImpl;
import com.only4play.codegen.processor.updater.GenUpdater;
import com.only4play.codegen.processor.vo.GenVo;
import com.only4play.common.constants.ValidStatus;
import com.only4play.jpa.converter.ValidStatusConverter;
import com.only4play.jpa.support.BaseJpaAggregate;

import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.only4play.productcenter.infrastructre.constants.ProductErrorCode;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Objects;

@GenVo(pkgName = "com.only4play.productcenter.domain.goods.vo")
@GenCreator(pkgName = "com.only4play.productcenter.domain.goods.creator")
@GenUpdater(pkgName = "com.only4play.productcenter.domain.goods.updater")
@GenRepository(pkgName = "com.only4play.productcenter.domain.goods.repository")
@GenService(pkgName = "com.only4play.productcenter.domain.goods.service")
@GenServiceImpl(pkgName = "com.only4play.productcenter.domain.goods.service")
@GenQuery(pkgName = "com.only4play.productcenter.domain.goods.query")
@GenMapper(pkgName = "com.only4play.productcenter.domain.goods.mapper")
@GenController(pkgName = "com.only4play.productcenter.web.controller")
@GenCreateRequest(pkgName = "com.only4play.productcenter.api.goods.request", sourcePath = Constants.GEN_API_SOURCE)
@GenUpdateRequest(pkgName = "com.only4play.productcenter.api.goods.request", sourcePath = Constants.GEN_API_SOURCE)
@GenQueryRequest(pkgName = "com.only4play.productcenter.api.goods.request", sourcePath = Constants.GEN_API_SOURCE)
@GenResponse(pkgName = "com.only4play.productcenter.api.goods.response", sourcePath = Constants.GEN_API_SOURCE)
@GenFeign(pkgName = "com.only4play.productcenter.api.goods.service", sourcePath = Constants.GEN_API_SOURCE, serverName = "product-srv")
@Entity
@Table(name = "goods")
@Data
public class Goods extends BaseJpaAggregate {
  @FieldDesc(name = "商品名称")
  private String name;
  @FieldDesc(name = "规格ID")
  private Long skuId;
  @FieldDesc(name = "唯一编码")
  private String uniqueCode;
  @FieldDesc(name = "价格")
  private BigDecimal price;
  @FieldDesc(name = "批次号")
  private String batchNo;
  @FieldDesc(name = "仓库ID")
  private Long storeId;

  @Convert(converter = ValidStatusConverter.class)
  @IgnoreUpdater
  @IgnoreCreator
  private ValidStatus validStatus;



  /**
   * 入库
   */
  private void in(){
    if(!Objects.equals(getValidStatus(),ValidStatus.INVALID)){
      throw new BusinessException(ProductErrorCode.GOODS_HAS_IN);
    }
    setValidStatus(ValidStatus.VALID);
  }


  /**
   * 出库
   */
  private void out(){
    if(!Objects.equals(getValidStatus(),ValidStatus.VALID)){
      throw new BusinessException(ProductErrorCode.GOODS_HAS_OUT);
    }
    setValidStatus(ValidStatus.INVALID);
  }
  public void init() {
    setValidStatus(ValidStatus.VALID);
  }

  public void valid() {
    setValidStatus(ValidStatus.VALID);
  }

  public void invalid() {
    setValidStatus(ValidStatus.INVALID);
  }
}
